﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using QASampling.BusinessObject;
using QASampling.UI;

namespace QASampling.QAControl
{
    public class QAEditButton : System.Windows.Forms.Button
    {
        private string tableSource = "";
        public string TableSource
        {
            get { return tableSource; }
            set { tableSource = value; }
        }

        private string fieldName = "";
        public string FieldName
        {
            get { return fieldName; }
            set { fieldName = value; }
        }

        private string fieldLabel = "";
        public string FieldLabel
        {
            get { return fieldLabel; }
            set { fieldLabel = value; }
        }

        private int fieldLength;
        public int FieldLength
        {
            get { return fieldLength; }
            set { fieldLength = value; }
        }

        private JenisField fieldType;
        public JenisField FieldType
        {
            get { return fieldType; }
            set { fieldType = value; }
        }

        private string dateFormat;
        public string DateFormat
        {
            get { return dateFormat; }
            set { dateFormat = value; }
        }

        public QAEditButton()
        {
            InitializeComponent();
        }

        private int digits = 0;
        public int Digits
        {
            get { return digits; }
            set { digits = value; }
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // QAEditButton
            // 
            this.BackgroundImage = global::QASampling.Properties.Resources.Edit;
            this.Size = new System.Drawing.Size(19, 19);
            this.TabStop = false;
            this.Click += new System.EventHandler(this.QAEditButton_Click);
            this.ResumeLayout(false);

        }

        void QAEditButton_Click(object sender, EventArgs e)
        {
            if (fieldName.Length == 0)
            {
                throw new Exception(this.Name + " FieldName belum diisi");
            }
            try
            {
                using (frmQAEdit f = new frmQAEdit())
                {
                    f.FieldName = fieldName;
                    if (this.Parent.Controls[this.Parent.Controls.IndexOfKey(fieldName)] is CheckBox)
                        f.FieldValue = ((QACheckBox)this.Parent.Controls[this.Parent.Controls.IndexOfKey(fieldName)]).Checked.ToString();
                    else
                        f.FieldValue = this.Parent.Controls[this.Parent.Controls.IndexOfKey(fieldName)].Text;
                    f.TableSource = tableSource;
                    f.FieldLabel = fieldLabel;
                    f.FieldLength = fieldLength;
                    f.FieldType = fieldType;
                    f.DateFormat = dateFormat;
                    f.Digits = digits;
                    f.ShowDialog(this);
                }
            }
            catch (Exception)
            {
                throw new Exception("Field tidak ditemukan");
            }
        }
    }
}
